package org.light.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.TableStringUtil;
import org.light.utils.WriteableUtil;

public class ExistsByName extends Verb implements EasyUIPositions {
	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("Exists" + this.domain.getStandardName() + "By"
					+ StringUtil.capFirst(this.domain.getDomainName().getFieldName()));
			method.setNoContainer(false);
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.addSignature(new Signature(2, this.domain.getDomainName().getLowerFirstFieldName(),this.domain.getDomainName().getFieldType()));
			method.setReturnType(new Type("bool"));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			
			if (StringUtil.isBlank(this.getDbType())||this.getDbType().equalsIgnoreCase("MariaDB")||this.getDbType().equalsIgnoreCase("MySQL")) {
				sList.add(new Statement(1000L,1,"sql := \"select count(*) countNum from "+ domain.getDbPrefix() + TableStringUtil.domainNametoTableName(domain) +" where "+StringUtil.changeDomainFieldtoTableColum(domain.getDomainName().getFieldName())+" = ?;\""));
			}else if (this.getDbType().equalsIgnoreCase("PostgreSQL")||this.getDbType().equalsIgnoreCase("pgsql")) {
				sList.add(new Statement(1000L,1,"sql := \""+ "select count(*) countNum from "+ domain.getDbPrefix() + TableStringUtil.domainNametoTableName(domain) +" where "+StringUtil.changeDomainFieldtoTableColum(domain.getDomainName().getFieldName())+" = $1\""));
			}else if (this.getDbType().equalsIgnoreCase("Oracle")) {
				sList.add(new Statement(1000L,1,"sql := \""+ "select count(*) countNum from "+ domain.getDbPrefix() + TableStringUtil.domainNametoTableName(domain) +" where "+StringUtil.changeDomainFieldtoTableColum(domain.getDomainName().getFieldName())+" = :"+domain.getDomainName().getFieldName()+" \""));
			}	
			
			sList.add(new Statement(2000L,1,"stmt, err := db.Prepare(sql)"));
			sList.add(new Statement(2500L,1,"defer stmt.Close()"));
			sList.add(new Statement(3000L,1,"if err != nil {"));
			sList.add(new Statement(4000L,2,"panic(err)"));
			sList.add(new Statement(5000L,1,"}"));
			sList.add(new Statement(6000L,1,"row := stmt.QueryRow("+this.domain.getDomainName().getLowerFirstFieldName()+")"));
			sList.add(new Statement(7000L,1,"var countNum int64"));

			long serial = 8000L;
			sList.add(new Statement(serial,0,""));
			sList.add(new Statement(serial+1000L,1,"err = row.Scan(&countNum)"));
			sList.add(new Statement(serial+2000L,1,"if err != nil {"));
			sList.add(new Statement(serial+3000L,2,"panic(err)"));
			sList.add(new Statement(serial+4000L,1,"}"));
			sList.add(new Statement(serial+5000L,1,"return countNum > 0"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("Exists" + this.domain.getStandardName() + "By"
					+ StringUtil.capFirst(this.domain.getDomainName().getFieldName()));
			method.setReturnType(new Type(this.domain.getDomainSuffix()+"."+this.domain.getType()));
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.addSignature(new Signature(2, this.domain.getDomainName().getFieldName(), new Type("String")));
			method.setThrowException(true);

			return method;
		}
	}
	
	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(
					"Exists" + this.domain.getStandardName() + "By" + this.domain.getDomainName().getCapFirstFieldName());
			method.setReturnType(new Type("bool"));
			method.addSignature(new Signature(1, this.domain.getDomainName().getFieldName(), new Type("string")));

			Method daomethod = this.generateDaoMethodDefinition();

			List<Writeable> list = new ArrayList<Writeable>();
			list.add(new Statement(1000L,1,"db := database.DB"));
			list.add(new Statement(2000L, 1, "return "
					+ daomethod.generateStandardDaoImplCallString(this.domain.getDaoimplSuffix())));
			method.setMethodStatementList(WriteableUtil.merge(list));

			return method;
		}
	}

	public ExistsByName(Domain domain)  throws ValidateException{
		super();
		this.domain = domain;
		this.setVerbName("Exists" + this.domain.getStandardName() + "By"
				+ StringUtil.capFirst(this.domain.getDomainName().getFieldName()));
		this.setLabel("是否存在");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("Exists");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		return null;
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;
	}
	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("Exists" + this.domain.getStandardName() + "By"
					+ StringUtil.capFirst(this.domain.getDomainName().getFieldName()));
			method.setNoContainer(false);
			method.addSignature(new Signature(1, "db","*sql.DB"));
			method.addSignature(new Signature(2, this.domain.getDomainName().getLowerFirstFieldName(),this.domain.getDomainName().getFieldType()));
			method.setReturnType(new Type("bool"));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,1,"return true"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		return generateServiceImplMethod();
	}
}
